<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :background="background"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  total: number
  page?: number
  limit?: number
  pageSizes?: Array<number>
  layout?: string
  background?: boolean
  hidden?: boolean
}

const emits = defineEmits<{
  'update:page': [page: number]
  'update:limit': [limit: number]
  pagination: [value: any]
}>()

const props = withDefaults(defineProps<Props>(), {
  page: 1,
  limit: 20,
  pageSizes: () => [10, 20, 30, 50],
  layout: 'total, sizes, prev, pager, next, jumper',
  background: true,
  hidden: false
})

const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emits('update:page', val)
  }
})

const pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    return emits('update:limit', val)
  }
})

const handleSizeChange = (val) => {
  emits('pagination', { pages: currentPage.value, limit: val })
}

const handleCurrentChange = (val) => {
  emits('pagination', { pages: val, limit: pageSize.value })
}
</script>

<style lang="scss" scoped>
.pagination-container {
  background: #fff;
  padding: 20px 16px 0;
}
.pagination-container.hidden {
  display: none;
}
</style>
